
#include "dtmf.h"
#include "LPC23xx.h"
#include "io.h"
#include "debug.h"


#define   PIN_StD				PORT_2_PIN.PIN_8
#define   PIN_StD_DIR		PORT_2_DIR.PIN_8
#define   DTMF_REC_PORT		FIO2PIN

#define   DTMF_PORT_MASK	((unsigned int)0x0f<<4)

#define   PIN_DEBOUNCE		100

unsigned int pin_StD_state;
unsigned int pin_StD_filter;
unsigned int pin_StD_event;



void dtmf_init(void)
{
	PIN_StD_DIR = PIN_INPUT;
	PORT_2_DIR.PIN_3 = PIN_INPUT; /* for new MEC */
	PORT_2_PIN.PIN_3 = 0;	
} 

int dtmf_digit;

void dtmf_monitor ( void )
{
	dtmf_digit = DTMF_EMPTY;

	if ( pin_StD_state == PIN_STATE_LO )
	{
		if ( PIN_StD )
		{
			if ( ++pin_StD_filter >= PIN_DEBOUNCE )
			{
				pin_StD_state = PIN_STATE_HI;
				pin_StD_filter = 0;
				pin_StD_event = PIN_EVENT__UP;
					
			}
		}
		else
		{
			pin_StD_filter = 0;
		}
	}
	else
	{
		if ( !PIN_StD )
		{
			if ( ++pin_StD_filter >= PIN_DEBOUNCE )
			{
				pin_StD_state = PIN_STATE_LO;
				pin_StD_filter = 0;
				pin_StD_event = PIN_EVENT__DOWN;
			}
		}
		else
		{
			pin_StD_filter = 0;
		}
	}	

	if ( 	pin_StD_event != PIN_EVENT__UP )
		return;

	pin_StD_event = PIN_EVENT__NO;

	dtmf_digit = DTMF_REC_PORT; 
	dtmf_digit &= DTMF_PORT_MASK;
	dtmf_digit >>=4;

	if ( dtmf_digit == DTMF_0 )
		dtmf_digit -= DTMF_0;

	DBG("\n%s; dtmf_digit = %d\n",__FUNCTION__,dtmf_digit); 
}
